package com.ldy.chat.cli;

import com.ldy.chat.service.ChatCliService;
import com.ldy.chat.service.ChatServerService;
import com.ldy.chat.service.Msg;
import com.ldy.chat.service.User;
import com.ldy.chat.service.impl.ChatCliServiceImpl;
import com.ldy.chat.service.impl.ChatServerServiceImpl;
import com.ldy.chat.test.cli1.CliServer;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TJSONProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.server.TNonblockingServer;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import org.apache.thrift.transport.layered.TFastFramedTransport;
import org.apache.thrift.transport.layered.TFramedTransport;

import java.util.Scanner;

/**
 * @Auther lin DeYU
 * @Date 2021/3/30
 */
public class ChatCli   {

    private int PORT=8080;

    public  void setPORT(int port){
        this.PORT=port;
    }

    public int getPORT(){
        return this.PORT;
    }

    // 获取构建聊天本地客户端服务器
    public TNonblockingServer getChatCliServer() throws TTransportException {
        // 设置通信监听端口
        TNonblockingServerSocket tNonblockingServerSocket=new TNonblockingServerSocket(PORT);
        ChatCliService.Processor processor =
                new ChatCliService.Processor<ChatCliService.Iface>(new ChatCliServiceImpl());
        // 数据序列化的方式
        TJSONProtocol.Factory protocolFactory = new  TJSONProtocol.Factory();
        TNonblockingServer.Args targs=new TNonblockingServer.Args(tNonblockingServerSocket);
        //设置远程调用的过程
        targs.processor(processor);
        // 传输数据序列化的协议
        targs.protocolFactory(protocolFactory);
        //设置数据传输的方式
        targs.transportFactory(new TFastFramedTransport.Factory());

        return new TNonblockingServer(targs);
    }

    // 客户端向服务端发送上线请求
   public void online(User user) throws TException {
       TTransport tTransport = new TFramedTransport(new TSocket("localhost",9000));
       // 设置数据传输协议,就是传输什么样的数据，怎样进行序列化和反序列化
       TProtocol tProtocol = new TJSONProtocol(tTransport);
       ChatServerService.Client chatServiceService=new ChatServerService.Client(tProtocol);
       tTransport.open();
       System.out.println(chatServiceService.online(user));
       tTransport.close();
   }


    // 客户端向服务端发送下线请求
   public void offine(User user) throws TException {
       TTransport tTransport = new TFramedTransport(new TSocket("localhost",9000));
       // 设置数据传输协议,就是传输什么样的数据，怎样进行序列化和反序列化
       TProtocol tProtocol = new TJSONProtocol(tTransport);
       ChatServerService.Client chatServiceService=new ChatServerService.Client(tProtocol);
       tTransport.open();
       System.out.println(chatServiceService.offine(user));
       tTransport.close();
   }


   // 客户端向服务端发送消息
   public void sendMsg(User user, Msg msg) throws TException {
       TTransport tTransport = new TFramedTransport(new TSocket("localhost",this.PORT));
       // 设置数据传输协议,就是传输什么样的数据，怎样进行序列化和反序列化
       TProtocol tProtocol = new TJSONProtocol(tTransport);
       ChatCliService.Client chatCliService=new ChatCliService.Client(tProtocol);
       tTransport.open();
       chatCliService.sendMsg(user,msg);
       tTransport.close();
   }


}
